package com.css.fxfzypg.manage.disasterrisk.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzypg.base.attachment.service.AttachmentInfoService;
import com.css.fxfzypg.manage.disasterrisk.repository.DisasterRiskReqository;
import com.css.fxfzypg.manage.disasterrisk.repository.entity.DisasterRiskEntity;
import com.css.fxfzypg.manage.disasterrisk.service.DisasterRiskService;
import com.css.fxfzypg.manage.disasterrisk.vo.DisasterRiskParams;
import com.css.fxfzypg.modules.surveytaskmanage.entity.FieldSurveyTaskEntity;
import com.css.fxfzypg.util.*;
import com.css.fxfzypg.zcpt.sys.service.SysAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.*;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

@Slf4j
@Service
public class DisasterRiskServiceImpl implements DisasterRiskService {

    @Autowired
    DisasterRiskReqository disasterRiskReqository;

    @Autowired
    AttachmentInfoService attachmentInfoService;

    @Autowired
    SysAreaService sysAreaService;

    /*@Override
    public Map<String, Object> checkPoint(String x,String y) {
        //标记的调研点
        Point point = new Point();
        point.setLocation(new Double(x),new Double(y));
        Map<String, Object> map = new HashMap<>();
        //获取任务表中所有任务区域
        List<FieldSurveyTaskEntity> fieldSurveyTaskEntities = disasterRiskReqository.getAllTaskRegion();
        for(int i = 0; i < fieldSurveyTaskEntities.size(); i++){
            Point[] points = new Point[i];
            FieldSurveyTaskEntity fieldSurveyTaskEntity = fieldSurveyTaskEntities.get(i);
            String taskRegion = fieldSurveyTaskEntity.getTaskRegion();
            List<Point> pointList = new ArrayList<>(Arrays.asList(points));
            for(String region : taskRegion.split(";")){
                Double regionX = new Double(region.split(",")[0].trim());
                Double regionY = new Double(region.split(",")[1].trim());
                Point regionPoint = new Point();
                regionPoint.setLocation(regionX,regionY);
                pointList.add(regionPoint);
            }
            //任务中选中的区域
            Point[] regionPointArr = pointList.toArray(points);
            //判断该灾害风险点是否在任务区域中
            boolean pointInPolygon = AreaBoundaryUtil.isPointInPolygon(point, regionPointArr);
            if(pointInPolygon){
                map.put("result","true");
                map.put("taskCode",fieldSurveyTaskEntity.getTaskNum());
                break;
            }
        }
        if(map.size() <= 0){
            map.put("result","false");
            map.put("taskCode","");
        }
        return map;
    }*/

    @Override
    public boolean saveDisasterRisk(DisasterRiskEntity disasterRiskEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        disasterRiskEntity.setId(uuid);
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(disasterRiskEntity.getProvince(), "UTF-8"), URLEncoder.encode(disasterRiskEntity.getCity(), "UTF-8"), URLEncoder.encode(disasterRiskEntity.getCounty(), "UTF-8"));
        disasterRiskEntity.setProvinceId(divisionIds.split("_")[0]);
        disasterRiskEntity.setCityId(divisionIds.split("_")[1]);
        disasterRiskEntity.setCountyId(divisionIds.split("_")[2]);
        disasterRiskEntity.setCreateTime(new Date());
        disasterRiskEntity.setIsSubmit("0");
        disasterRiskEntity.setDelFlag("0");
        return disasterRiskReqository.saveDisasterRisk(disasterRiskEntity);
    }

    @Override
    public boolean saveAndSubmit(DisasterRiskEntity disasterRiskEntity) throws UnsupportedEncodingException {
        String uuid = UUIDGenerator.getUUID();
        disasterRiskEntity.setId(uuid);
        String divisionIds = sysAreaService.getIdByCondition(URLEncoder.encode(disasterRiskEntity.getProvince(), "UTF-8"), URLEncoder.encode(disasterRiskEntity.getCity(), "UTF-8"), URLEncoder.encode(disasterRiskEntity.getCounty(), "UTF-8"));
        disasterRiskEntity.setProvinceId(divisionIds.split("_")[0]);
        disasterRiskEntity.setCityId(divisionIds.split("_")[1]);
        disasterRiskEntity.setCountyId(divisionIds.split("_")[2]);
        disasterRiskEntity.setIsSubmit("1");
        disasterRiskEntity.setDelFlag("0");
        disasterRiskEntity.setCreateTime(new Date());
        disasterRiskEntity.setSubmitter(disasterRiskEntity.getCreateName());
        disasterRiskEntity.setSubmitterId(disasterRiskEntity.getCreateId());
        disasterRiskEntity.setSubmitTime(new Date());
        return disasterRiskReqository.saveDisasterRisk(disasterRiskEntity);
    }


    @Override
    public Map<String,Object>  querySubmitOrNot(String submitFlag, int curPage, int pageSize, String userId) {
        Map<String,Object> map = disasterRiskReqository.querySubmitOrNot(submitFlag,curPage,pageSize,userId);
        return map;
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean updateIsSubmit(Map<String,String> map) {
        return disasterRiskReqository.updateIsSubmit(map);
    }

    @Override
    public DisasterRiskEntity queryData(String id) {
        DisasterRiskEntity disasterRiskEntity = disasterRiskReqository.queryData(id);
        return disasterRiskEntity;
    }

    @Override
    public boolean modifyData(DisasterRiskEntity disasterRiskEntity) {
        return disasterRiskReqository.modifyData(disasterRiskEntity);
    }

    @Override
    public boolean modifyAndSubmit(DisasterRiskEntity disasterRiskEntity) {
        disasterRiskEntity.setIsSubmit("1");
        disasterRiskEntity.setSubmitter(disasterRiskEntity.getCreateName());
        disasterRiskEntity.setSubmitterId(disasterRiskEntity.getCreateId());
        disasterRiskEntity.setSubmitTime(new Date());
        return disasterRiskReqository.modifyData(disasterRiskEntity);
    }

    @Transactional(rollbackFor = Exception.class )
    @Override
    public boolean deleteDisasterRisk(String ids) {
        boolean res = true;
        for(String id : ids.split(",")){
            res = disasterRiskReqository.deleteDisasterRisk(id);
            if(res == false){
                break;
            }
        }
        return res;
    }



    @Override
    public Map<String, Object> queryDisasterData(DisasterRiskParams queryParams, int curPage, int pageSize) {
        /*String taskCode = queryParams.getTaskCode();
        StringBuilder taskCodes = new StringBuilder();
        JSONObject jsonObject = new JSONObject();
        if(!PlatformObjectUtils.isEmpty(taskCode)) {
            //获取任务表中所有任务区域
            List<FieldSurveyTaskEntity> fieldSurveyTaskEntities = disasterRiskReqository.getTaskRegionByTaskCode(taskCode);
            //获取调研点所有的经纬度
            List<DisasterRiskEntity> allPoint = disasterRiskReqository.getAllPoint();
            List<Point> pointList = new ArrayList<>(Arrays.asList());
            for (int i = 0; i < fieldSurveyTaskEntities.size(); i++) {
            //for (DisasterRiskEntity disasterRiskEntity : allPoint) {
                Point[] points = new Point[i];
                FieldSurveyTaskEntity fieldSurveyTaskEntity = fieldSurveyTaskEntities.get(i);
                String taskRegion = fieldSurveyTaskEntity.getTaskRegion();
                for (String region : taskRegion.split(";")) {
                    Double regionX = new Double(region.split(",")[0].trim());
                    Double regionY = new Double(region.split(",")[1].trim());
                    Point regionPoint = new Point();
                    regionPoint.setLocation(regionX, regionY);
                    pointList.add(regionPoint);
                }
                for (DisasterRiskEntity disasterRiskEntity : allPoint) {
                //for (int i = 0; i < fieldSurveyTaskEntities.size(); i++) {
                    String latitude = disasterRiskEntity.getLatitude();
                    String longitude = disasterRiskEntity.getLongitude();
                    Point point = new Point();
                    point.setLocation(new Double(latitude), new Double(longitude));
                    //任务中选中的区域
                    Point[] regionPointArr = pointList.toArray(points);
                    boolean pointInPolygon = AreaBoundaryUtil.isPointInPolygon(point, regionPointArr);
                    if (pointInPolygon) {
                        jsonObject.put("","");
                        //taskCodes.append(fieldSurveyTaskEntity.getTaskNum() + ",");
                    }
                }
            }
        }*/
        Map<String, Object> map = disasterRiskReqository.queryDisasterData(queryParams,curPage,pageSize);
        return map;
    }

}
